package com.example.servlet.bookServlet;



import com.example.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deletebook")
public class BookDeleteServlet extends HttpServlet {
    private BookDao bookDao = new BookDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

            // 获取要删除的书籍ID
            String idStr = request.getParameter("id");
            int id = Integer.parseInt(idStr.trim());

            // 调用DAO删除书籍
            boolean success = bookDao.deleteBookById(id);

            if (success) {
                // 删除成功，
                response.sendRedirect("index.jsp");
            } else {
                // 删除失败
                request.setAttribute("error", "删除失败");
                request.getRequestDispatcher("bookList").forward(request, response);
            }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
